﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using EntityLibrary;
using System.Collections;
using System.Data;

namespace EntityDaoLibrary
{
    public class IRalationInHosRegDAO : IEntityDAO
    {
        private IDataAccess data;
        public IRalationInHosRegDAO()
        {
        }
        public IRalationInHosRegDAO(IDataAccess data)
        {
            this.data = data;
        }


        #region IEntityDAO 成员

        public void insertEntity(DataSet entityData)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                sqlStruct.SqlString = @"INSERT INTO I_RALATIONINHOSREG (
                                                                INHOSFLOWNO,INHOSID,MEDTYPE,MEDICARENO,IDCARDNO,FLAG,TRANDT,REMARK,
                                                                PRE0,PRE1,PRE2,PRE3,PRE4,PRE5,PRE6,PRE7,PRE8,PRE9,PRE10,PRE11,PRE12,PRE13,PRE14,PRE15,OUTPUTSTR,MEDCLASS,POSDATE,POSMONEY,CASHMONEY,POSFLOWNO
                                                                ) values (@INHOSFLOWNO,@INHOSID,@MEDTYPE,@MEDICARENO,@IDCARDNO,@FLAG,@TRANDT,@REMARK,@PRE0,@PRE1,@PRE2,@PRE3,@PRE4,@PRE5,@PRE6,@PRE7,@PRE8,@PRE9,@PRE10,@PRE11,@PRE12,@PRE13,@PRE14,@PRE15,@OUTPUTSTR,@MEDCLASS,@POSDATE,@POSMONEY,@CASHMONEY,@POSFLOWNO)";
                pars = new ParamField[30];
                pars[0] = new ParamField("@INHOSFLOWNO", "INHOSFLOWNO");
                pars[1] = new ParamField("@INHOSID", "INHOSID");
                pars[2] = new ParamField("@MEDTYPE", "MEDTYPE");
                pars[3] = new ParamField("@MEDICARENO", "MEDICARENO");
                pars[4] = new ParamField("@IDCARDNO", "IDCARDNO");
                pars[5] = new ParamField("@FLAG", "FLAG");
                pars[6] = new ParamField("@TRANDT", "TRANDT");
                pars[7] = new ParamField("@REMARK", "REMARK");
                pars[8] = new ParamField("@PRE0", "PRE0");
                pars[9] = new ParamField("@PRE1", "PRE1");
                pars[10] = new ParamField("@PRE2", "PRE2");
                pars[11] = new ParamField("@PRE3", "PRE3");
                pars[12] = new ParamField("@PRE4", "PRE4");
                pars[13] = new ParamField("@PRE5", "PRE5");
                pars[14] = new ParamField("@PRE6", "PRE6");
                pars[15] = new ParamField("@PRE7", "PRE7");
                pars[16] = new ParamField("@PRE8", "PRE8");
                pars[17] = new ParamField("@PRE9", "PRE9");

                pars[18] = new ParamField("@PRE10", "PRE10");
                pars[19] = new ParamField("@PRE11", "PRE11");
                pars[20] = new ParamField("@PRE12", "PRE12");
                pars[21] = new ParamField("@PRE13", "PRE13");
                pars[22] = new ParamField("@PRE14", "PRE14");
                pars[23] = new ParamField("@PRE15", "PRE15");
                pars[24] = new ParamField("@OUTPUTSTR", "OUTPUTSTR");
                pars[25] = new ParamField("@MEDCLASS", "MEDCLASS");
                pars[26] = new ParamField("@POSDATE", "POSDATE");
                pars[27] = new ParamField("@POSMONEY", "POSMONEY");
                pars[28] = new ParamField("@CASHMONEY", "CASHMONEY");
                pars[29] = new ParamField("@POSFLOWNO", "POSFLOWNO");

                sqlStruct.ParamFields = pars;
                foreach (DataRow row in entityData.Tables["I_RALATIONINHOSREG"].Rows)
                {
                    data.ExecuteNonQuery(row, sqlStruct);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public void updateEntity(DataSet entityData)
        {
            try
            {
                if (entityData.GetChanges() == null)
                    return;
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                foreach (DataRow row in entityData.GetChanges().Tables["I_RALATIONINHOSREG"].Rows)
                {
                    switch (row.RowState)
                    {
                        case DataRowState.Added:
                            sqlStruct.SqlString = @"INSERT INTO I_RALATIONINHOSREG (
                                                                INHOSFLOWNO,INHOSID,MEDTYPE,MEDICARENO,IDCARDNO,FLAG,TRANDT,REMARK,
                                                                PRE0,PRE1,PRE2,PRE3,PRE4,PRE5,PRE6,PRE7,PRE8,PRE9,PRE10,PRE11,PRE12,PRE13,PRE14,PRE15,OUTPUTSTR,MEDCLASS,POSDATE,POSMONEY,CASHMONEY,POSFLOWNO
                                                                ) values (@INHOSFLOWNO,@INHOSID,@MEDTYPE,@MEDICARENO,@IDCARDNO,@FLAG,@TRANDT,@REMARK,@PRE0,@PRE1,@PRE2,@PRE3,@PRE4,@PRE5,@PRE6,@PRE7,@PRE8,@PRE9,@PRE10,@PRE11,@PRE12,@PRE13,@PRE14,@PRE15,@OUTPUTSTR,@MEDCLASS,@POSDATE,@POSMONEY,@CASHMONEY,@POSFLOWNO)";
                            pars = new ParamField[30];
                            pars[0] = new ParamField("@INHOSFLOWNO", "INHOSFLOWNO");
                            pars[1] = new ParamField("@INHOSID", "INHOSID");
                            pars[2] = new ParamField("@MEDTYPE", "MEDTYPE");
                            pars[3] = new ParamField("@MEDICARENO", "MEDICARENO");
                            pars[4] = new ParamField("@IDCARDNO", "IDCARDNO");
                            pars[5] = new ParamField("@FLAG", "FLAG");
                            pars[6] = new ParamField("@TRANDT", "TRANDT");
                            pars[7] = new ParamField("@REMARK", "REMARK");
                            pars[8] = new ParamField("@PRE0", "PRE0");
                            pars[9] = new ParamField("@PRE1", "PRE1");
                            pars[10] = new ParamField("@PRE2", "PRE2");
                            pars[11] = new ParamField("@PRE3", "PRE3");
                            pars[12] = new ParamField("@PRE4", "PRE4");
                            pars[13] = new ParamField("@PRE5", "PRE5");
                            pars[14] = new ParamField("@PRE6", "PRE6");
                            pars[15] = new ParamField("@PRE7", "PRE7");
                            pars[16] = new ParamField("@PRE8", "PRE8");
                            pars[17] = new ParamField("@PRE9", "PRE9");

                            pars[18] = new ParamField("@PRE10", "PRE10");
                            pars[19] = new ParamField("@PRE11", "PRE11");
                            pars[20] = new ParamField("@PRE12", "PRE12");
                            pars[21] = new ParamField("@PRE13", "PRE13");
                            pars[22] = new ParamField("@PRE14", "PRE14");
                            pars[23] = new ParamField("@PRE15", "PRE15");
                            pars[24] = new ParamField("@OUTPUTSTR", "OUTPUTSTR");
                            pars[25] = new ParamField("@MEDCLASS", "MEDCLASS");
                            pars[26] = new ParamField("@POSDATE", "POSDATE");
                            pars[27] = new ParamField("@POSMONEY", "POSMONEY");
                            pars[28] = new ParamField("@CASHMONEY", "CASHMONEY");
                            pars[29] = new ParamField("@POSFLOWNO", "POSFLOWNO");


                            sqlStruct.ParamFields = pars;
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                        case DataRowState.Deleted:
                            sqlStruct.SqlString = "DELETE FROM I_RALATIONINHOSREG WHERE INHOSFLOWNO=@INHOSFLOWNO";
                            pars = new ParamField[1];
                            pars[0] = new ParamField("@INHOSFLOWNO", "INHOSFLOWNO");
                            sqlStruct.ParamFields = pars;
                            row.RejectChanges();
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                        case DataRowState.Modified:
                            sqlStruct.SqlString = @"UPDATE I_RALATIONINHOSREG SET 
                                                                    INHOSID=@INHOSID,MEDTYPE=@MEDTYPE, MEDICARENO=@MEDICARENO,IDCARDNO=@IDCARDNO,FLAG=@FLAG,TRANDT=@TRANDT,REMARK=@REMARK,
                                                                    PRE0=@PRE0,PRE1=@PRE1,PRE2=@PRE2,PRE3=@PRE3,PRE4=@PRE4,PRE5=@PRE5,PRE6=@PRE6,PRE7=@PRE7,PRE8=@PRE8,PRE9=@PRE9,
                                                                    PRE10=@PRE10,PRE11=@PRE11,PRE12=@PRE12,PRE13=@PRE13,PRE14=@PRE14,PRE15=@PRE15,OUTPUTSTR=@OUTPUTSTR,MEDCLASS=@MEDCLASS,POSDATE=@POSDATE,POSMONEY=@POSMONEY,CASHMONEY=@CASHMONEY,POSFLOWNO=@POSFLOWNO
                                                                    WHERE INHOSFLOWNO=@INHOSFLOWNO";
                            pars = new ParamField[30];
                            pars[0] = new ParamField("@INHOSFLOWNO", "INHOSFLOWNO");
                            pars[1] = new ParamField("@INHOSID", "INHOSID");
                            pars[2] = new ParamField("@MEDTYPE", "MEDTYPE");
                            pars[3] = new ParamField("@MEDICARENO", "MEDICARENO");
                            pars[4] = new ParamField("@IDCARDNO", "IDCARDNO");
                            pars[5] = new ParamField("@FLAG", "FLAG");
                            pars[6] = new ParamField("@TRANDT", "TRANDT");
                            pars[7] = new ParamField("@REMARK", "REMARK");
                            pars[8] = new ParamField("@PRE0", "PRE0");
                            pars[9] = new ParamField("@PRE1", "PRE1");
                            pars[10] = new ParamField("@PRE2", "PRE2");
                            pars[11] = new ParamField("@PRE3", "PRE3");
                            pars[12] = new ParamField("@PRE4", "PRE4");
                            pars[13] = new ParamField("@PRE5", "PRE5");
                            pars[14] = new ParamField("@PRE6", "PRE6");
                            pars[15] = new ParamField("@PRE7", "PRE7");
                            pars[16] = new ParamField("@PRE8", "PRE8");
                            pars[17] = new ParamField("@PRE9", "PRE9");
                            pars[18] = new ParamField("@PRE10", "PRE10");
                            pars[19] = new ParamField("@PRE11", "PRE11");
                            pars[20] = new ParamField("@PRE12", "PRE12");
                            pars[21] = new ParamField("@PRE13", "PRE13");
                            pars[22] = new ParamField("@PRE14", "PRE14");
                            pars[23] = new ParamField("@PRE15", "PRE15");
                            pars[24] = new ParamField("@OUTPUTSTR", "OUTPUTSTR");
                            pars[25] = new ParamField("@MEDCLASS", "MEDCLASS");
                            pars[26] = new ParamField("@POSDATE", "POSDATE");
                            pars[27] = new ParamField("@POSMONEY", "POSMONEY");
                            pars[28] = new ParamField("@CASHMONEY", "CASHMONEY");
                            pars[29] = new ParamField("@POSFLOWNO", "POSFLOWNO");

                            sqlStruct.ParamFields = pars;
                            data.ExecuteNonQuery(row, sqlStruct);
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public void deleteEntity(DataSet entityData)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                ParamField[] pars;
                sqlStruct.SqlString = "DELETE FROM I_RALATIONINHOSREG WHERE INHOSFLOWNO=@INHOSFLOWNO";
                pars = new ParamField[1];
                pars[0] = new ParamField("@INHOSFLOWNO", "INHOSFLOWNO");
                sqlStruct.ParamFields = pars;
                foreach (DataRow row in entityData.Tables["I_RALATIONINHOSREG"].Rows)
                {
                    data.ExecuteNonQuery(row, sqlStruct);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        public DataSet FindByPrimaryKey(object strKeyValue)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                string sql = "";
                QueryParameterCollection para;
                IRalationInHosRegData ds = new IRalationInHosRegData();
                sql = "SELECT I_RALATIONINHOSREG.* from I_RALATIONINHOSREG WHERE INHOSFLOWNO=@INHOSFLOWNO";
                para = new QueryParameterCollection(1);
                para.Add("@INHOSFLOWNO", strKeyValue);
                data.ExecuteDataset(sql, para, ds, "I_RALATIONINHOSREG");
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


        #endregion
        public DataSet FindByPrimaryKey(ArrayList KeyValue)
        {
            try
            {
                SqlStruct sqlStruct = new SqlStruct();
                string sql = "";
                QueryParameterCollection para;
                IRalationInHosRegData ds = new IRalationInHosRegData();
                sql = "SELECT I_RALATIONINHOSREG.* FROM I_RALATIONINHOSREG WHERE INHOSFLOWNO=@INHOSFLOWNO";
                para = new QueryParameterCollection(1);
                para.Add("@INHOSFLOWNO", KeyValue[0]);
                data.ExecuteDataset(sql, para, ds, "I_RALATIONINHOSREG");
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

        //根据住院ID查询医保对应的流水号和医院交易流水号
        public DataSet FindByInHosID(string inhosId)
        {
            try
            {
                DataSet ds = new DataSet();
                QueryParameterCollection qpc = new QueryParameterCollection(1);
                string sql = "SELECT I_RALATIONINHOSREG.* FROM I_RALATIONINHOSREG WHERE INHOSID=:INHOSTID";
                qpc.Add(":INHOSTID", inhosId);
                data.ExecuteDataset(sql, qpc, ds, "I_RALATIONINHOSREG");
                return ds;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }


    }
}
